package com._dev.social.service.impl;

import com._dev.social.common.ErrorCode;
import com._dev.social.exception.BusinessException;
import com._dev.social.mode.domain.Users;
import com._dev.social.service.FollowService;
import com._dev.social.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Objects;
import java.util.Set;

import static com._dev.social.contant.UserConstant.FOLLOWED_KEY_PREFIX;
import static com._dev.social.contant.UserConstant.FOLLOW_KEY_PREFIX;

@Service
public class FollowServiceImpl implements FollowService {
    @Autowired
    private UsersService usersService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    // 关注用户
    public void followUser(String userId, String followUserId) {
        // 将关注的用户加入到当前用户的关注列表中
        redisTemplate.opsForSet().add(FOLLOW_KEY_PREFIX + userId, followUserId);
        // 将当前用户加入到被关注的用户的被关注列表中
        redisTemplate.opsForSet().add(FOLLOWED_KEY_PREFIX + followUserId, userId);
    }

    // 获取用户的关注列表
    public Set<String> getFollowList(String userId) {

        Set<String> followList = redisTemplate.opsForSet().members(FOLLOW_KEY_PREFIX + userId);
        return followList != null ? followList : Collections.emptySet();
    }

    // 获取用户的被关注列表
    public Set<String> getFollowedList(String userId) {
        Set<String> followedList = redisTemplate.opsForSet().members(FOLLOWED_KEY_PREFIX + userId);
        return  followedList!=null?followedList:Collections.emptySet();
    }

    // 取消关注
    public void unfollowUser(String userId, String unfollowUserId) {
        redisTemplate.opsForSet().remove(FOLLOW_KEY_PREFIX + userId, unfollowUserId);
        redisTemplate.opsForSet().remove(FOLLOWED_KEY_PREFIX + unfollowUserId, userId);
    }
}
